//
//  OutlineDownloadController.m
//  History
//
//  Created by 七 on 16/9/5.
//  Copyright © 2016年 kane. All rights reserved.
//

#import "OutlineDownloadController.h"
#import "ArticleCountController.h"
#import "ChannelSelectController.h"
@interface OutlineDownloadController ()<UITableViewDataSource,UITableViewDelegate>
{
    __weak IBOutlet UIView *navView;
    __weak IBOutlet UILabel *titleLbl;
    __weak IBOutlet UILabel *lineLbl;
    __weak IBOutlet UITableView *downloadTable;
    NSMutableArray *channelAry;
    NSMutableArray *selectedList;
}
@end

@implementation OutlineDownloadController

- (void)viewDidLoad {
    [super viewDidLoad];
    navView.backgroundColor = KKColor_DarkGray_White;
    titleLbl.textColor = KKColor_Black_LightGary;
    lineLbl.backgroundColor = KKColor_NavLineColor;
    downloadTable.backgroundColor = KKColor_DarkWhite_Black;
    channelAry = [NSMutableArray array];
    selectedList = [NSMutableArray array];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectchanged) name:@"DounloadCount" object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectchanged) name:@"ChangeChannel" object:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) selectchanged
{
    [downloadTable reloadData];
}

#pragma mark - User Action

- (IBAction)backPress:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark UITableViewDelegate & datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    }else{
        return 20;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"离线内容";
    }else{
        return @"选择频道";
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identify = [NSString stringWithFormat:@"%ld%ld",(long)indexPath.section,(long)indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
    }
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.backgroundColor = KKColor_DarkGray_White;
    cell.textLabel.textColor = KKColor_Black_LightGary;
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:{
                cell.textLabel.text = @"每频道下载文章篇数";
                UILabel *countLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 250, 60, 40)];
                switch ([[[NSUserDefaults standardUserDefaults] objectForKey:@"DownCount"] integerValue]) {
                    case 0:
                        countLbl.text = @"10";
                        break;
                    case 1:
                        countLbl.text = @"20";
                        break;
                    case 2:
                        countLbl.text = @"30";
                        break;
                    case 3:
                        countLbl.text = @"40";
                        break;
                    case 4:
                        countLbl.text = @"50";
                        break;
                    case 5:
                        countLbl.text = @"100";
                        break;
                    default:
                        break;
                };
                countLbl.textColor = [UIColor colorWithHex:0X46C09A];
                countLbl.textAlignment = NSTextAlignmentRight;
                countLbl.font = [UIFont systemFontOfSize:13];
                cell.accessoryView = countLbl;
            }break;
                
            default:{
                cell.textLabel.text = @"离线频道选择";
                UILabel *selectLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 240, 70, 40)];
                if ([[NSUserDefaults standardUserDefaults] objectForKey:@"ChannelSelected"] == nil) {
                    selectLbl.text = @"全部频道";
                }else{
                    switch ([[[NSUserDefaults standardUserDefaults] objectForKey:@"ChannelSelected"] integerValue]) {
                        case 0:
                            selectLbl.text = @"全部频道";
                            break;
                        case 1:
                            selectLbl.text = @"手动选择";
                            break;
                        default:
                            break;
                    }
                }
                selectLbl.textColor = [UIColor colorWithHex:0X46C09A];
                selectLbl.textAlignment = NSTextAlignmentRight;
                selectLbl.font = [UIFont systemFontOfSize:13];
                cell.accessoryView = selectLbl;
            }break;
        }
    }else{
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"ChannelSelected"] integerValue] == 0) {
            [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
        }else{
//            if (channelAry.count > 0) {
//                for (int i=0; i<channelAry.count; i++) {
//                    NSLog(@"%ld,%@",indexPath.row,channelAry[i]);
//                    if (indexPath.row == (NSInteger)channelAry[i]) {
//                        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
//                    } else{
//                        [cell setAccessoryType:UITableViewCellAccessoryNone];
//                    }
//                }
//            }else{
//                [cell setAccessoryType:UITableViewCellAccessoryNone];
//            }
            
        }
        switch (indexPath.row) {
            case 0:
                cell.textLabel.text = @"推荐";
                break;
            case 1:
                cell.textLabel.text = @"健康";
                break;
            case 2:
                cell.textLabel.text = @"上班族健康";
                break;
            case 3:
                cell.textLabel.text = @"养生";
                break;
            case 4:
                cell.textLabel.text = @"中医";
                break;
            case 5:
                cell.textLabel.text = @"减肥";
                break;
            case 6:
                cell.textLabel.text = @"女性健康";
                break;
            case 7:
                cell.textLabel.text = @"男性健康";
                break;
            case 8:
                cell.textLabel.text = @"糖尿病";
                break;
            case 9:
                cell.textLabel.text = @"月经";
                break;
            case 10:
                cell.textLabel.text = @"壮阳";
                break;
            case 11:
                cell.textLabel.text = @"按摩推拿";
                break;
            case 12:
                cell.textLabel.text = @"抗癌";
                break;
            case 13:
                cell.textLabel.text = @"心理健康";
                break;
            case 14:
                cell.textLabel.text = @"急救知识";
                break;
            case 15:
                cell.textLabel.text = @"食品安全";
                break;
            case 16:
                cell.textLabel.text = @"亚健康";
                break;
            case 17:
                cell.textLabel.text = @"经络";
                break;
            case 18:
                cell.textLabel.text = @"心脑血管疾病";
                break;
            case 19:
                cell.textLabel.text = @"食补食疗";
                break;
            default:
                break;
        }
    }
    return cell;
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:{
                ArticleCountController *articleVC = [[ArticleCountController alloc] init];
                [self.navigationController pushViewController:articleVC animated:YES];
            }break;
            case 1:{
                ChannelSelectController *channelVC = [[ChannelSelectController alloc] init];
                [self.navigationController pushViewController:channelVC animated:YES];
            }break;
            default:
                break;
        }
    }else{
//        for (int i=0; i<indexPath.row; i++) {
//            if (channelAry.count>0) {
//                if (indexPath.row == (NSInteger)channelAry[i]) {
//                    [channelAry removeObjectAtIndex:i];
//                }
//            }else{
//                [channelAry addObject:@(indexPath.row)];
//            }
//            
//        }
    }
    [downloadTable reloadData];
}


@end
